package com.danielagarza.app;

import java.util.List;
import java.util.concurrent.ExecutionException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.danielagarza.app.modelos.Noticia;
import com.danielagarza.app.servicios.LeerJSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Noticias extends Fragment {

	private ExpandableListView lvwNoticias;
	private ExpandableListAdapter mAdapter;

	public Noticias() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		View vista = inflater.inflate(R.layout.activity_noticias, container, false);
		lvwNoticias = (ExpandableListView) vista.findViewById(R.id.expandableListView1);
		
		
		CargarDatos t = new CargarDatos();
		t.execute();
		try {
			//txt.setText(""+t.get());
			Gson g = new Gson();
			List<Noticia> l = g.fromJson(t.get(), new TypeToken<List<Noticia>>(){}.getType());
			mAdapter = new ListAdapter(vista,l);
			lvwNoticias.setAdapter(mAdapter);
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return vista;
	}

	private class CargarDatos extends AsyncTask<Void, Integer, String> {
		@Override
		protected String doInBackground(Void... arg0) {
			String valor = LeerJSON.leerJSON("http://li530-140.members.linode.com:9003/servicios/noticias");
			return valor;
		}
	}

	public class ListAdapter extends BaseExpandableListAdapter {

		private View vista;
		private List<Noticia> lstNoticias;
	

		public ListAdapter(View vista, List<Noticia> lstNoticias) {
			this.vista=vista;
			this.lstNoticias = lstNoticias;
		}
		
		public Object getChild(int groupPosition, int childPosition) {
			return lstNoticias.get(groupPosition).cuerpo;
		}

		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		public int getChildrenCount(int groupPosition) {
			return 1;
		}

		public TextView getGenericView() {
			// Layout parameters for the ExpandableListView
			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT, 64);

			TextView textView = new TextView(vista.getContext());
			textView.setLayoutParams(lp);
			// Center the text vertically
			textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
			// Set the text starting position
			textView.setPadding(40, 0, 0, 0);
			return textView;
		}

		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
			TextView textView = getGenericView();
			textView.setText(getChild(groupPosition, childPosition).toString());
			return textView;
		}

		public Object getGroup(int groupPosition) {
			return lstNoticias.get(groupPosition);
		}

		public int getGroupCount() {
			return lstNoticias.size();
		}

		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
			TextView textView = getGenericView();
			textView.setText(getGroup(groupPosition).toString());
			return textView;
		}

		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		public boolean hasStableIds() {
			return true;
		}



	}

}
